const state = {
	userInfo: uni.getStorageSync('userInfo') ? uni.getStorageSync('userInfo') : null,
	permissions: uni.getStorageSync('permissions') ? uni.getStorageSync('permissions') : [],
};
const mutations = {
	setUserInfo(state, param) {
		state.userInfo = param;
		if (null === param) {
			uni.removeStorageSync('token');
			uni.removeStorageSync('userInfo');
		} else {
			uni.setStorageSync('userInfo', param);
		}
	},

	updateUserInfo(state, param) {
		let userInfo = state.userInfo;
		state.userInfo = Object.assign(userInfo, param);
		uni.setStorageSync('userInfo', state.userInfo);
	},
	setPermissions(state, permissions) {
		state.permissions = permissions;
		uni.setStorageSync('permissions', state.permissions);
	}
};

import {
	getUserInfo,
} from '@/api/login.js'
const actions = {
	actionGetUserInfo({
		commit,
		state
	}) {
		return new Promise((resolve, reject) => {
			getUserInfo().then((res) => {
				if (state.userInfo) {
					commit('updateUserInfo', res.data.user);
				} else {
					commit('setUserInfo', res.data.user);
				}
				commit('setPermissions', res.data.permissions)
				resolve(true)
			}).catch((res) => {
				reject(false)
			})
		})
	},
};


export default {
	state,
	mutations,
	actions
}